package com.list;

import com.tgy.util.List;

import java.util.EmptyStackException;


/**
 * @Author: tgy
 * @Date: 2020/9/2 9:06 下午
 */
public class Stack<E>  {

  private List<E> dataList;

  public Stack() {

    dataList = new LinkedList<>();
  }

  /**
   * Pushes an item onto the top of this stack. This has exactly
   * the same effect as:
   * <blockquote><pre>
   * addElement(item)</pre></blockquote>
   *
   * @param   item   the item to be pushed onto this stack.
   * @return  the <code>item</code> argument.
   * @see     java.util.Vector#addElement
   */
  public E push(E item) {

    dataList.add(0,item);
    return item;
  }

  /**
   * Removes the object at the top of this stack and returns that
   * object as the value of this function.
   *
   * @return  The object at the top of this stack (the last item
   *          of the <tt>Vector</tt> object).
   * @throws EmptyStackException  if this stack is empty.
   */
  public  E pop() {

    return dataList.remove(0);
  }

  /**
   * Looks at the object at the top of this stack without removing it
   * from the stack.
   *
   * @return  the object at the top of this stack (the last item
   *          of the <tt>Vector</tt> object).
   * @throws  EmptyStackException  if this stack is empty.
   */
  public  E peek() {

    return dataList.get(0);
  }

  /**
   * Tests if this stack is empty.
   *
   * @return  <code>true</code> if and only if this stack contains
   *          no items; <code>false</code> otherwise.
   */
  public boolean empty() {

    return dataList.isEmpty();
  }

  @Override
  public String toString() {
    return "Stack{" +
      "dataList=" + dataList +
      '}';
  }
}
